package com.tang.transmission.controller;


import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import com.tang.common.entity.liveVideo.VideoInfo;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;
import com.tang.transmission.service.VideoInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-17
 */
@RestController
@RequestMapping("/video")
public class VideoInfoController {
    @Autowired
    private VideoInfoService videoInfoService;

    @RequestMapping("/preachVideoList")
    public Result getVideoList(String keywords, Integer currentPage, Integer pageSize,Integer hasAuth) {
        if(hasAuth==null){
            hasAuth=1;
        }
        System.out.println(hasAuth);
        PageInfo<VideoInfo> list = videoInfoService.listVideoInfo(keywords,currentPage,pageSize,hasAuth);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }
    @RequestMapping("/getVideo")
    public Result getVideo(Integer videoId) {
        VideoInfo video = videoInfoService.getById(videoId);
        if(video!=null){
            return Result.success(video);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }

    @RequestMapping("/companyVideoManage")
    public Result getVideoList(String keywords, Integer currentPage, Integer pageSize,String companyName) {
        if(companyName ==null){
            return  Result.error(RecruitExceptionEnum.NO_DATA);
        }
        PageInfo<VideoInfo> list = videoInfoService.listCompanyVideo(keywords,currentPage,pageSize,companyName);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }
    @RequestMapping("/delPreachVideo")
    public Result delVideoById(String videoId) {
        VideoInfo videoInfo = videoInfoService.getById(videoId);
        videoInfo.setDeleteFlag(0);
        boolean result = videoInfoService.updateById(videoInfo);
        if(result){
            return Result.success("删除成功");
        }else{
            return Result.error(RecruitExceptionEnum.DELETE_FAILED);
        }
    }
    @ApiOperation("审核宣讲视频")
    @RequestMapping("/auditPreachVideo")
    public Result auditVideo(Integer videoId,String res){
        String result = videoInfoService.auditVideo(videoId,res);
        if (result != null) {
            return Result.success(result);
        } else {
            return Result.error(RecruitExceptionEnum.AUDIT_FAILED);
        }
    }

    @RequestMapping("/upload")
    public Result auditVideo(
            String companyName,
            String userName,
            String coverUrl,
            String videoUrl,
            String title,
            String content,
            String speaker
    ){
       if(title!=null && companyName !=null && userName!=null){
           VideoInfo videoInfo = new VideoInfo();
           videoInfo.setVideoIntroduce(content);
           videoInfo.setVideoUrl(videoUrl);
           videoInfo.setCoverUrl(coverUrl);
           videoInfo.setUserName(userName);
           videoInfo.setInstitutions(companyName);
           videoInfo.setVideoTitle(title);
           videoInfo.setSpeaker(speaker);
           boolean save = videoInfoService.save(videoInfo);
           if(save){
               return Result.success("上传成功");
           }
       }
        return Result.error(RecruitExceptionEnum.UPLOAD_FAILED);
    }


    @RequestMapping("/update")
    public Result updateVideo( @RequestParam("videoId")Integer videoId,
                               @RequestParam("title") String title,
                               @RequestParam("content")  String content,
                               @RequestParam("speaker") String speaker,
                               @RequestParam("coverUrl") String coverUrl){
        VideoInfo video = videoInfoService.getById(videoId);
        if(video!=null){
            video.setVideoTitle(title);
            video.setVideoIntroduce(content);
            video.setSpeaker(speaker);
            video.setCoverUrl(coverUrl);
            boolean b = videoInfoService.updateById(video);
            if(b){
                return  Result.success("修改成功");
            }
        }
        return Result.error(RecruitExceptionEnum.UPDATE_FAILED);
    }
    @RequestMapping("/videoCount")
    public Integer getVideoCount(){
        return  videoInfoService.count(null);
    }
}
